package diploma.kisel.modelas.domain;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 * Domain class for Database (table - app_database).
 * 
 * @author Nikolay Kisel
 *
 */
@Entity
@Table(name = "app_database")
@NamedQueries({
	@NamedQuery(name = "Database.findAll", query = "select d from Database d"),
	@NamedQuery(name = "Database.findById", query = "select d from Database d where d.id = :id")})
public class Database {

	private Integer id;
	private String number;
	private DatabaseType type;
	private Integer countOfTables;
	private Integer countOfCreationDays;
	private Integer dataCapacity;
	private Integer archieveDataCapacity;
	private Integer cost;
	
	//for many to many mapping
	private Set<Workstation> workstations = new HashSet<Workstation>();
	private Set<Workplace> workplaces = new HashSet<Workplace>();
	
	public Database() {
		
	}
	
	public Database(String number, DatabaseType type, Integer countOfTables,
			Integer countOfCreationDays, Integer dataCapacity,
			Integer archieveDataCapacity) {
		this.number = number;
		this.type = type;
		this.countOfTables = countOfTables;
		this.countOfCreationDays = countOfCreationDays;
		this.dataCapacity = dataCapacity;
		this.archieveDataCapacity = archieveDataCapacity;
	}

	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id_database")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "number")
	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	@Column(name = "type")
	@Enumerated(EnumType.STRING)
	public DatabaseType getType() {
		return type;
	}

	public void setType(DatabaseType type) {
		this.type = type;
	}

	@Column(name = "count_tables")
	public Integer getCountOfTables() {
		return countOfTables;
	}

	public void setCountOfTables(Integer countOfTables) {
		this.countOfTables = countOfTables;
	}

	@Column(name = "count_creation_days")
	public Integer getCountOfCreationDays() {
		return countOfCreationDays;
	}

	public void setCountOfCreationDays(Integer countOfCreationDays) {
		this.countOfCreationDays = countOfCreationDays;
	}

	@Column(name = "data_volume")
	public Integer getDataCapacity() {
		return dataCapacity;
	}

	public void setDataCapacity(Integer dataCapacity) {
		this.dataCapacity = dataCapacity;
	}

	@Column(name = "achive_data_volume")
	public Integer getArchieveDataCapacity() {
		return archieveDataCapacity;
	}

	public void setArchieveDataCapacity(Integer archieveDataCapacity) {
		this.archieveDataCapacity = archieveDataCapacity;
	}

	@ManyToMany
	@JoinTable(name = "list_cdb", joinColumns = @JoinColumn(name = "id_database"), inverseJoinColumns = @JoinColumn(name = "id_workstation"))
	public Set<Workstation> getWorkstations() {
		return workstations;
	}

	public void setWorkstations(Set<Workstation> workstations) {
		this.workstations = workstations;
	}	

	@Column(name = "cost")
	public Integer getCost() {
		return cost;
	}

	public void setCost(Integer cost) {
		this.cost = cost;
	}

	@ManyToMany
	@JoinTable(name = "list_ldb", joinColumns = @JoinColumn(name = "id_database"), inverseJoinColumns = @JoinColumn(name = "id_workplace"))
	public Set<Workplace> getWorkplaces() {
		return workplaces;
	}

	public void setWorkplaces(Set<Workplace> workplaces) {
		this.workplaces = workplaces;
	}

	@Override
	public String toString() {
		return "Database [id=" + id + ", number=" + number + ", type=" + type
				+ ", countOfTables=" + countOfTables + ", countOfCreationDays="
				+ countOfCreationDays + ", dataCapacity=" + dataCapacity
				+ ", archieveDataCapacity=" + archieveDataCapacity + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((number == null) ? 0 : number.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Database other = (Database) obj;
		if (number == null) {
			if (other.number != null)
				return false;
		} else if (!number.equals(other.number))
			return false;
		return true;
	}	

}
